#include "Player.h"
#include "SDL.h"
#include "SDL_opengl.h"
#include "Vector2.h"
#include "SDL_image.h"
#include "TextureManager.h"

#include <vector>
#include <iostream>
#include <cmath>

using namespace std;

bool facingRight = true;
bool airborne = false;

const int WALKING[2] = {3, 4};
const int FIRING[2] = {7, 10};
const int row = 2;

Player::Player(const Map* map): scale(1.0f), posX(3.0f), posY(20.0f), xVel(0.0f), yVel(0.0f), frameLength(200), frame(0)
{
	this->map = map;
	animationTick = SDL_GetTicks();
	sheet = TextureManager::getInstance()->getSpriteSheet("arc2.png");
//	
//	//glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL); 
//	//glEnable(GL_TEXTURE_2D);
//	SDL_Surface* imgFile = IMG_Load("mario.png");
//	if (!imgFile)
//		std::cout << "failed to load player image" << std::endl;
//	
//	glGenTextures(1, &texture);
//	glBindTexture(GL_TEXTURE_2D, texture);
//	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
//	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
//
//	float imgW = imgFile->w;
//	float imgH = imgFile->h;
//	
//	glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,imgW,imgH,0,GL_RGBA,GL_UNSIGNED_BYTE,imgFile->pixels);
//	SDL_FreeSurface(imgFile);
	float imgW,imgH;
	imgW = sheet->getTileWidth();
	imgH = sheet->getTileHeight();
	
	if (imgW > imgH)
	{
		height = (scale * imgH) / imgW;
		width = scale;
	}
	else
	{
		width = (scale * imgW) / imgH;
		height = scale;
	}
}

void Player::draw()
{
	glEnable(GL_TEXTURE_2D);
	
	glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	glEnable (GL_BLEND);
	
	glBindTexture(GL_TEXTURE_2D, sheet->getTextureInt());
	glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

	// flip texture
	glMatrixMode(GL_TEXTURE);
	glPushMatrix();
	
	TexCoords* tc = sheet->getTexCoords(row, WALKING[frame]);
	
	GLfloat leftS, rightS = 0.0f;
	if ( facingRight )
	{
		leftS = tc->s + tc->unitS;
		rightS = tc->s;
	}
	else
	{
		leftS = tc->s;
		rightS = tc->s + tc->unitS;
	}

	
	
	glBegin(GL_QUADS);
		glTexCoord2f(rightS, tc->t + tc->unitT);
		glVertex2f(width + posX,height + posY);
		glTexCoord2f(rightS, tc->t);
		glVertex2f(width + posX,posY);
		glTexCoord2f(leftS, tc->t);
		glVertex2f(posX,posY);
		glTexCoord2f(leftS, tc->t + tc->unitT);
		glVertex2f(posX,height + posY);
	glEnd();

	glPopMatrix();	// Pop texture matrix

	glDisable(GL_BLEND);
}

void Player::update()
{
	posX += xVel;
	if (map->collision(*this))
	{
		posX -= xVel;
	}

	yVel -= GRAVITY;
	posY += yVel;
	if (map->collision(*this))
	{
		posY -= yVel;
		if (yVel < 0)
		{
			airborne = false;
		}
		yVel = 0;
	}
	
	if (yVel != 0)
		airborne = true;
	
	animate();
}

void Player::handleEvent(SDL_Event* event)
{
	
	if (event->type == SDL_KEYDOWN)
	{
		switch( event->key.keysym.sym )
		{

		case SDLK_UP:
			if (!airborne)
			{
				yVel += 0.2f;
				airborne = true;
			}
			
			break;
		case SDLK_DOWN:
			//yVel -= SPEED;
			break;
		case SDLK_LEFT:
			facingRight = false;
			xVel -= SPEED;
			break;
		case SDLK_RIGHT:
			facingRight = true;
			xVel += SPEED;
			break;
		}
	}
	else if (event->type == SDL_KEYUP)
	{
		switch( event->key.keysym.sym )
		{
			case SDLK_UP:
			//yVel -= SPEED;
			break;
		case SDLK_DOWN:
			//yVel += SPEED;
			break;
		case SDLK_LEFT:
			xVel += SPEED;
			break;
		case SDLK_RIGHT:
			xVel -= SPEED;
			break;
		}
	}
}

Vector2 Player::getPosition() const
{
	return Vector2(posX, posY);
}

Rect Player::getSize() const
{
	return Rect(width, height);
}

void Player::animate()
{
	if ( (SDL_GetTicks() - animationTick) < frameLength )
		return;

	frame++;
	if (frame > 1 || xVel == 0 || airborne)
		frame = 0;
	animationTick = SDL_GetTicks();
}